package com.luoqiz.file.util.aliyun;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.sql.Date;
import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.common.utils.BinaryUtil;
import com.aliyun.oss.model.GetObjectRequest;
import com.aliyun.oss.model.MatchMode;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PolicyConditions;
import com.aliyun.oss.model.PutObjectRequest;
import com.aliyun.oss.model.PutObjectResult;

public class AliyunOSSUtils {

	String accessKeyId = "LTAI2V2eoFcpv88B";
	String accessKeySecret = "OuPMMbjmeFZJv1wuoxN1aBEiuEDzY7";
	String endpoint = "oss-cn-hongkong.aliyuncs.com";
	String bucketName = "luoqiz";
	String host = "http://" + bucketName + "." + endpoint; // host的格式为 bucketname.endpoint

	// callbackUrl为 上传回调服务器的URL，请将下面的IP和Port配置为您自己的真实信息。
	String callbackUrl = "http://test.luoqiz.top:8888";

	static String dir = "boya/"; // 用户上传文件时指定的前缀。

	public AliyunOSSUtils() {
	}

	public AliyunOSSUtils(String accessKeyId, String accessKeySecret, String endpoint, String bucketName,
			String callbackUrl) {
		this.accessKeyId = accessKeyId; // 请填写您的AccessKeyId。
		this.accessKeySecret = accessKeySecret; // 请填写您的AccessKeySecret。
		this.endpoint = endpoint; // 请填写您的 endpoint。
		this.bucketName = bucketName; // 请填写您的 bucketname 。
		this.callbackUrl = callbackUrl; // 上传回调服务器的URL
	}

	/**
	 * 获取oss客户端连接
	 * 
	 * @return
	 */
	public OSS getOSSClient() {
		return new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);

	}

	public void closeOSSClient(OSS oss) {
		oss.shutdown();
	}

	/**
	 * 获取前端临时上传文件凭证
	 * 
	 * @param callback
	 * @return
	 */
	public String getAcessToken(String callback, boolean clientShutdown) {

		OSS client = getOSSClient();
		try {
			long expireTime = 30;
			long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
			Date expiration = new Date(expireEndTime);
			PolicyConditions policyConds = new PolicyConditions();
			policyConds.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
			policyConds.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);

			String postPolicy = client.generatePostPolicy(expiration, policyConds);
			byte[] binaryData = postPolicy.getBytes("utf-8");
			String encodedPolicy = BinaryUtil.toBase64String(binaryData);
			String postSignature = client.calculatePostSignature(postPolicy);

			Map<String, String> respMap = new LinkedHashMap<String, String>();
			respMap.put("accessid", accessKeyId);
			respMap.put("policy", encodedPolicy);
			respMap.put("signature", postSignature);
			respMap.put("dir", dir);
			respMap.put("host", host);
			respMap.put("expire", String.valueOf(expireEndTime / 1000));
			// respMap.put("expire", formatISO8601Date(expiration));

			JSONObject jasonCallback = new JSONObject();
			jasonCallback.put("callbackUrl", callbackUrl);
			jasonCallback.put("callbackBody",
					"filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
			jasonCallback.put("callbackBodyType", "application/x-www-form-urlencoded");
			String base64CallbackBody = BinaryUtil.toBase64String(jasonCallback.toString().getBytes());
			respMap.put("callback", base64CallbackBody);

			JSONObject ja1 = JSONObject.parseObject(JSONObject.toJSONString(respMap));
			return response(callback, ja1.toString());
		} catch (Exception e) {
			// Assert.fail(e.getMessage());
			System.out.println(e.getMessage());
		} finally {
			if (clientShutdown) {
				client.shutdown();
			}
		}
		return null;
	}

	/**
	 * 服务器响应结果
	 * 
	 * @return
	 */
	private static String response(String callbackFunName, String results) {
		if (callbackFunName == null || callbackFunName.equalsIgnoreCase(""))
			return results;
		else
			return callbackFunName + "( " + results + " )";
	}

	/**
	 * 获取public key
	 * 
	 * @param url
	 * @return
	 */
//	@SuppressWarnings({ "finally" })
//	public String executeGet(String url) {
//		BufferedReader in = null;
//
//		String content = null;
//		try {
//			// 定义HttpClient
//			@SuppressWarnings("resource")
//			DefaultHttpClient client = new DefaultHttpClient();
//			// 实例化HTTP方法
//			HttpGet request = new HttpGet();
//			request.setURI(new URI(url));
//			HttpResponse response = client.execute(request);
//
//			in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
//			StringBuffer sb = new StringBuffer("");
//			String line = "";
//			String NL = System.getProperty("line.separator");
//			while ((line = in.readLine()) != null) {
//				sb.append(line + NL);
//			}
//			in.close();
//			content = sb.toString();
//		} catch (Exception e) {
//		} finally {
//			if (in != null) {
//				try {
//					in.close();// 最后要关闭BufferedReader
//				} catch (Exception e) {
//					e.printStackTrace();
//				}
//			}
//			return content;
//		}
//	}

	/**
	 * 获取Post消息体
	 * 
	 * @param is
	 * @param contentLen
	 * @return
	 */
//	public String GetPostBody(InputStream is, int contentLen) {
//		if (contentLen > 0) {
//			int readLen = 0;
//			int readLengthThisTime = 0;
//			byte[] message = new byte[contentLen];
//			try {
//				while (readLen != contentLen) {
//					readLengthThisTime = is.read(message, readLen, contentLen - readLen);
//					if (readLengthThisTime == -1) {// Should not happen.
//						break;
//					}
//					readLen += readLengthThisTime;
//				}
//				return new String(message);
//			} catch (IOException e) {
//			}
//		}
//		return "";
//	}

	/**
	 * 验证上传回调的Request
	 * 
	 * @param request
	 * @param ossCallbackBody
	 * @return
	 * @throws NumberFormatException
	 * @throws IOException
	 */
//	protected boolean VerifyOSSCallbackRequest(HttpServletRequest request, String ossCallbackBody)
//			throws NumberFormatException, IOException {
//		boolean ret = false;
//		String autorizationInput = new String(request.getHeader("Authorization"));
//		String pubKeyInput = request.getHeader("x-oss-pub-key-url");
//		byte[] authorization = BinaryUtil.fromBase64String(autorizationInput);
//		byte[] pubKey = BinaryUtil.fromBase64String(pubKeyInput);
//		String pubKeyAddr = new String(pubKey);
//		if (!pubKeyAddr.startsWith("http://gosspublic.alicdn.com/")
//				&& !pubKeyAddr.startsWith("https://gosspublic.alicdn.com/")) {
//			System.out.println("pub key addr must be oss addrss");
//			return false;
//		}
//		String retString = executeGet(pubKeyAddr);
//		retString = retString.replace("-----BEGIN PUBLIC KEY-----", "");
//		retString = retString.replace("-----END PUBLIC KEY-----", "");
//		String queryString = request.getQueryString();
//		String uri = request.getRequestURI();
//		String decodeUri = java.net.URLDecoder.decode(uri, "UTF-8");
//		String authStr = decodeUri;
//		if (queryString != null && !queryString.equals("")) {
//			authStr += "?" + queryString;
//		}
//		authStr += "\n" + ossCallbackBody;
//		ret = doCheck(authStr, authorization, retString);
//		return ret;
//	}

	/**
	 * Post请求
	 */
//	protected void doPost(HttpServletRequest request, HttpServletResponse response)
//			throws ServletException, IOException {
//		String ossCallbackBody = GetPostBody(request.getInputStream(),
//				Integer.parseInt(request.getHeader("content-length")));
//		boolean ret = VerifyOSSCallbackRequest(request, ossCallbackBody);
//		System.out.println("verify result : " + ret);
//		// System.out.println("OSS Callback Body:" + ossCallbackBody);
//		if (ret) {
//			response(request, response, "{\"Status\":\"OK\"}", HttpServletResponse.SC_OK);
//		} else {
//			response(request, response, "{\"Status\":\"verdify not ok\"}", HttpServletResponse.SC_BAD_REQUEST);
//		}
//	}

	/**
	 * 验证RSA
	 * 
	 * @param content
	 * @param sign
	 * @param publicKey
	 * @return
	 */
//	public static boolean doCheck(String content, byte[] sign, String publicKey) {
//		try {
//			KeyFactory keyFactory = KeyFactory.getInstance("RSA");
//			byte[] encodedKey = BinaryUtil.fromBase64String(publicKey);
//			PublicKey pubKey = keyFactory.generatePublic(new X509EncodedKeySpec(encodedKey));
//			java.security.Signature signature = java.security.Signature.getInstance("MD5withRSA");
//			signature.initVerify(pubKey);
//			signature.update(content.getBytes());
//			boolean bverify = signature.verify(sign);
//			return bverify;
//
//		} catch (Exception e) {
//			e.printStackTrace();
//		}
//
//		return false;
//	}

	/**
	 * 服务器响应结果
	 * 
	 * @param request
	 * @param response
	 * @param results
	 * @param status
	 * @throws IOException
	 */
//	private void response(HttpServletRequest request, HttpServletResponse response, String results, int status)
//			throws IOException {
//		String callbackFunName = request.getParameter("callback");
//		response.addHeader("Content-Length", String.valueOf(results.length()));
//		if (callbackFunName == null || callbackFunName.equalsIgnoreCase(""))
//			response.getWriter().println(results);
//		else
//			response.getWriter().println(callbackFunName + "( " + results + " )");
//		response.setStatus(status);
//		response.flushBuffer();
//	}

//	/**
//	 * 服务器响应结果
//	 */
//	private static void response(HttpServletRequest request, HttpServletResponse response, String results)
//			throws IOException {
//		String callbackFunName = request.getParameter("callback");
//		if (callbackFunName == null || callbackFunName.equalsIgnoreCase(""))
//			response.getWriter().println(results);
//		else
//			response.getWriter().println(callbackFunName + "( " + results + " )");
//		response.setStatus(HttpServletResponse.SC_OK);
//		response.flushBuffer();
//	}

	/******************************************************* *******************************************************/

	/**
	 * 获取文件内的字符串
	 * 
	 * @param fileKey 文件
	 * @throws IOException
	 */
	public String getStringReadFile(String fileKey, Charset charset) throws IOException {
		OSS client = getOSSClient();
		OSSObject object = client.getObject(new GetObjectRequest(bucketName, fileKey));
		return getTextInputStream(object.getObjectContent(), charset);
	}

	/**
	 * 获取文件流
	 * 
	 * @param fileKey 文件
	 * @throws IOException
	 */
	public InputStream getStreamReadFile(String fileKey) {
		OSS client = getOSSClient();
		OSSObject object = client.getObject(new GetObjectRequest(bucketName, fileKey));
		return object.getObjectContent();

	}

	public File getTempFile(String fileKey) throws OSSException, ClientException, IOException {
		OSS client = getOSSClient();
		File file = File.createTempFile("oss-java-sdk-", "." + StringUtils.substringAfterLast(fileKey, "."));
		client.getObject(new GetObjectRequest(bucketName, fileKey), file);
		return file;

	}

	/**
	 * 获取oss文件對象
	 * 
	 * @param fileKey 文件
	 * @throws IOException
	 */
	public OSSObject getOSSObjectReadFile(String fileKey) {
		OSS client = getOSSClient();
		return client.getObject(new GetObjectRequest(bucketName, fileKey));
	}

	/**
	 * 从输入流中读取数据上传至阿里云oss
	 * 
	 * @param fileKey
	 * @param inputStream
	 * @return
	 * @throws IOException
	 */
	public PutObjectResult putInputStream(String fileKey, InputStream inputStream) throws IOException {
		OSS client = getOSSClient();
		return client.putObject(bucketName, fileKey, inputStream);

	}

	/**
	 * 文件上传至阿里云oss
	 * 
	 * @param fileKey
	 * @param file
	 * @return
	 * @throws IOException
	 */
	public PutObjectResult putFile(String fileKey, File file) throws IOException {
		OSS client = getOSSClient();
		return client.putObject(new PutObjectRequest(bucketName, fileKey, file));
	}

	/**
	 * 从输入流中读取字符串内容
	 * 
	 * @param input
	 * @param charset
	 * @return
	 * @throws IOException
	 */
	private String getTextInputStream(InputStream input, Charset charset) throws IOException {
		if (charset == null || !charset.isRegistered()) {
			charset = StandardCharsets.UTF_8;
		}
		StringBuilder sb = new StringBuilder();
		BufferedReader reader = new BufferedReader(new InputStreamReader(input, charset));
		while (true) {
			String line = reader.readLine();
			if (line == null)
				break;
			sb.append("\t" + line);
		}
		reader.close();
		return sb.toString();
	}
}
